package com.superdry.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.util.AbToastUtil;
import com.alibaba.fastjson.JSON;
import com.drysurper.R;
import com.superdry.adapter.AddressAdapter;
import com.superdry.json.Address;
import com.superdry.json.Userinfo;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.SPHelper;

public class AddressActivity extends Activity {

	ListView address_lv;
	TextView addAddress_tv;
	TextView back_tv;
	View goback;
	AddressAdapter addressAdapter;
	List<Address> addressInfoList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_address);
		initView();
		initData();
	}

	/**
	 * 初始化
	 */
	private void initData() {

		AbHttpUtil httpUtil = AbHttpUtil.getInstance(this);
		httpUtil.setTimeout(5000);
		AbRequestParams params = new AbRequestParams();
		params.put("userID", SPHelper.getUserInfo(this).getUserID() + "");

		httpUtil.post(GlobalConst.GET_ADDRESS_EXIST, params, new AbStringHttpResponseListener() {

			@Override
			public void onStart() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
				// TODO Auto-generated method stub
				AbToastUtil.showToast(AddressActivity.this, "获取信息失败");
			}

			@Override
			public void onSuccess(int statusCode, String content) {

				try {

					addressInfoList = JSON.parseArray(content, Address.class);
					if (addressInfoList != null && addressInfoList.size() > 0) {
						addressAdapter.updata(addressInfoList);
					} else {
						addressInfoList = new ArrayList<Address>();
					}

				} catch (Exception e) {
					// TODO: handle exception
				}

			}
		});

	}

	public void initView() {

		address_lv = (ListView) findViewById(R.id.address_lv);
		addAddress_tv = (TextView) findViewById(R.id.add_address);
		back_tv = (TextView) findViewById(R.id.back);
		goback = findViewById(R.id.goback);

		addressInfoList = new ArrayList<Address>();
		addressAdapter = new AddressAdapter(AddressActivity.this, addressInfoList);
		address_lv.setAdapter(addressAdapter);

		address_lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				Address address = addressAdapter.getItem(arg2);
				Intent data = new Intent();
				data.putExtra("address", address);
				setResult(0, data);
				finish();

			}
		});

		goback.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

		addAddress_tv.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent mIntent = new Intent();
				mIntent.setClass(AddressActivity.this, AddAddressActivity.class);
				startActivityForResult(mIntent, 0x00000000);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		initData();
	}

	public static class AddressInfo {

		public String name;
		public String tel;
		public String address;
	}
}
